package com.thwj.project.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.thwj.common.annotation.Log;
import com.thwj.common.core.controller.BaseController;
import com.thwj.common.core.domain.AjaxResult;
import com.thwj.common.core.page.TableDataInfo;
import com.thwj.common.enums.BusinessType;
import com.thwj.common.utils.StringUtils;
import com.thwj.common.utils.poi.ExcelUtil;
import com.thwj.project.biz.controller.vo.ActivityVO;
import com.thwj.project.biz.convert.UserInfoAdminResolver;
import com.thwj.project.biz.domain.Activity;
import com.thwj.project.biz.service.IActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动Controller
 *
 * @author generate
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/biz/activity")
public class ActivityController extends BaseController {
    @Autowired
    private IActivityService activityService;
    @Autowired
    private UserInfoAdminResolver userInfoAdminResolver;

/**
 * 查询活动列表
 */
@PreAuthorize("@ss.hasPermi('biz:activity:list')")
@GetMapping("/list")
    public TableDataInfo list(Activity activity) {
        startPage();
//        List<Activity> list = activityService.list(new QueryWrapper<>(activity));
        List<Activity> list = activityService.getBaseMapper().selectList(
                new QueryWrapper<Activity>()
                        .like(!StringUtils.isEmpty(activity.getNameCity()),"name_city", activity.getNameCity())
                        .like(!StringUtils.isEmpty(activity.getTitle()),"title", activity.getTitle())
                        .like(!StringUtils.isEmpty(activity.getContactPhone()),"contact_phone", activity.getContactPhone())
                        .eq(activity.getCategoryId() != null,"category_id", activity.getCategoryId())
        );
        //
        List<ActivityVO> activityVOS = list.stream().map(item->{
            ActivityVO activityVO = new ActivityVO();
            BeanUtils.copyProperties(item,activityVO);
            return activityVO;
        }).collect(Collectors.toList());
        userInfoAdminResolver.resolve(activityVOS,ActivityVO::getUserId,(obj, nickname, phone)->{
            obj.setNickName(nickname);
            obj.setUserPhone(phone);
        });
        return getDataTable(activityVOS,((Page)list).getTotal());
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('biz:activity:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Activity activity) {
        List<Activity> list = activityService.list(new QueryWrapper<>(activity));
        ExcelUtil<Activity> util = new ExcelUtil<Activity>(Activity. class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(activityService.getById(id));
    }

    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('biz:activity:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Activity activity) {
        return toAjax(activityService.save(activity));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('biz:activity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Activity activity) {
        return toAjax(activityService.updateById(activity));
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('biz:activity:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(activityService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 取消活动
     */
    @PreAuthorize("@ss.hasPermi('biz:activity:edit')")
    @Log(title = "取消活动", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody Activity activity) {
        return toAjax(activityService.cancel(activity));
    }

}
